//
//  PageView.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/5/31.
//

#import "PageView.h"
#import "CommonUtil.h"


@interface PageView()<UITableViewDelegate,UITableViewDataSource,IQActionSheetPickerViewDelegate>

@end

@implementation PageView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        
    }
    return self;
}

-(void)setType:(DetailPageType)type{
    _type = type;
    
    _bottomView = [[StoryBottomView alloc] initWithFrame:CGRectMake(0, self.height-AD(70), SCREEN_WIDTH, AD(70))];
    if (type == DetailPageTypeBigStory) {
        [self addSubview:self.topView];
        self.tableView.frame = CGRectMake(0, self
                                          .topView.bottom, SCREEN_WIDTH, self.height-self.topView.height-self.bottomView.height);
    }else if (type == DetailPageTypeGallery){
        self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.height-self.bottomView.height);
    }else if (type == DetailPageTypeDog || type == DetailPageTypeHumor || type == DetailPageTypeNorFowl){
        self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.height-self.bottomView.height);
    }
    
    [self addSubview:self.bottomView];
    [self addSubview:self.tableView];
    [self testData];

}





// 假数据
- (void)testData{
    self.models = [NSMutableArray array];
    for (NSInteger i = 0; i < 10; i++) {
        MessageModel *model = [[MessageModel alloc] init];
        model.content = @"根据国家卫健委24日下午公布的最新数据"; // 测试单行文字
        if (i < 10) {
            model.imageCount = i; //测试图片展示
        }
        if (i == 1 || i == 5) {
            model.content = @"环球时报记者 倪浩】根据国家卫健委24日下午公布的最新数据，截至5月23日，全国累计报告接种新冠病毒疫苗51085.8万剂次。在安徽和辽宁新一轮局部疫情之后，中国疫苗接种明显提速。5月19日之前的8天时间"; //测试多行文字
        }
        
        if (i == 0 || i == 6) {
            [model addReayTest];// 测试回复内容最多显示3条
        }

        [self.models addObject:model];
    }
    [self.tableView reloadData];
}



#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.isOnly) {
        return self.onlyModels.count;
    }else{
        return self.models.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LeaingMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.superVC = self.superVC;
    if (self.models.count > indexPath.row) {
        if (self.isOnly) {
            cell.model = self.onlyModels[indexPath.row];
        }else{
            cell.model = self.models[indexPath.row];
        }
        
    }
    
    return cell;
}

#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%zd",indexPath.row);
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isOnly) {
        MessageModel *model = self.onlyModels[indexPath.row];
        return model.cellHeight;
    }else{
        MessageModel *model = self.models[indexPath.row];
        return model.cellHeight;
    }
}

- (void)changeData:(UIButton *)sender{
    self.isOnly = [sender.titleLabel.text isEqualToString:@"只看楼主"];
    
    if (self.isOnly) {
        [CommonUtil setupPlaceholderView:self.tableView models:self.onlyModels];
    }else{
        [CommonUtil setupPlaceholderView:self.tableView models:self.models];
    }
    

    
    [self.tableView reloadData];
}


#pragma mark - IQActionSheetPickerViewDelegate

-(void)actionSheetPickerView:(IQActionSheetPickerView *)pickerView didSelectTitles:(NSArray *)titles{
    NSLog(@"%@",titles.firstObject);
}

- (void)refresh{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.mj_header endRefreshing];
    });
    
}


-(TopView *)topView{
    if (!_topView) {
        _topView = [[TopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, AD(70))];
    }
    return _topView;
}


-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        [_tableView registerClass:[LeaingMessageTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.headerView;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//iOS11系统及以上，
        } else {
           // self.automaticallyAdjustsScrollViewInsets = NO;
            // Fallback on earlier versions
        }
    }
    return _tableView;
}




-(StoryHeaderView *)headerView{
    if (!_headerView) {
        if (self.type != DetailPageTypeGallery) {
            _headerView = [[StoryHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, AD(712))];
        }else{
            _headerView = [[StoryHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, AD(616))];
        }
      
        _headerView.type = self.type;
        
        [_headerView.hotBtn addTarget:self action:@selector(changeData:) forControlEvents:UIControlEventTouchUpInside];
        [_headerView.onlyBtn addTarget:self action:@selector(changeData:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headerView;
}


@end
